package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.FocusUser;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;


    @Override
    public void save(Video video) {
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        video.setId(ObjectId.get());//？？？？为什么
        mongoTemplate.save(video);

    }

    @Override
    public List<Video> getByVids(List<Long> vids, Integer page, Integer pagesize) {

        Query query = new Query();
        query.addCriteria(Criteria.where("vid").in(vids))
                .with(PageRequest.of((page - 1), pagesize));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public List<Video> queryVideoList(Integer page, Integer pagesize) {
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.DESC, "created"))
                .skip(page - 1)
                .limit(pagesize);
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public void saveFollowUser(FocusUser focusUser) {
        mongoTemplate.save(focusUser);
    }

    @Override
    public Boolean exit(Long userId, Long followUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId)
                .and("followUserId").is(followUserId));
        return mongoTemplate.exists(query, FocusUser.class);
    }

    @Override
    public void deleteFollowUser(Long userId, Long followUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId)
                .and("followUserId").is(followUserId));
        mongoTemplate.remove(query, FocusUser.class);
    }

    @Override
    public Video getById(String videoId) {
        return mongoTemplate.findById(videoId,Video.class);
    }

    @Override
    public void updateLike(String vedioId, int i) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(new ObjectId(vedioId)));
        Update update = new Update();
        update.inc("likeCount", i);
        mongoTemplate.updateFirst(query, update, Video.class);
    }

    @Override
    public PageResult getByUserId(Integer page, Integer pagesize, Long uid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(uid))
                .with(PageRequest.of((page - 1), pagesize))
                .with(Sort.by(Sort.Direction.DESC, "created"));

        List<Video> videoList = mongoTemplate.find(query, Video.class);
        Long count = mongoTemplate.count(query, Video.class);
        return new PageResult(page, pagesize, count, videoList);
    }
}
